#导入模块
import pygame
from pygame.locals import*
import time,random

#添加键盘控制事件
def key_control(hero_temp):
	for event in pygame.event.get():
		if event.type==QUIT:
			print("exit")
			exit()


	#获取按键
	pressed_keys=pygame.key.get_pressed()
	#检测是否按下a或者left键
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print('left')
		hero_temp.move_left()
	#检测是否按下d或者right键
	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print('right')
		hero_temp.move_right()
    #检测是否按下w或者up键
	elif pressed_keys[K_UP] or pressed_keys[K_w]:
		print('up')
		hero_temp.move_up()
    #检测是否按下s或者down键
	elif pressed_keys[K_DOWN] or pressed_keys[K_s]:
		print('down')
		hero_temp.move_down()
	#检查是否是空格键
	if pressed_keys[K_SPACE]:
		print("space")
		hero_temp.fire()
#玩家定义
class HeroPlane:
	def __init__(self,screen_temp):
		self.x=200
		self.y=400
		self.image=pygame.image.load('./images/images/me.png')
		self.screen=screen_temp
		self.BulletList=[]
	def display(self):
		#子弹遍历显示
		for bullet in self.BulletList:
			bullet.display()
			#子弹越界清除
			if bullet.move():
				self.BulletList.remove(bullet)
		#玩家飞机显示
		self.screen.blit(self.image,(self.x,self.y))
	def move_left(self):
		self.x-=5
		if self.x<=0:
			self.x=0
	def move_right(self):
		self.x+=5
		if self.x>=406:
			self.x=406
	def move_up(self):
		self.y-=5
		if self.y<=5:
			self.y=5
	def move_down(self):
		self.y+=5
		if self.y>=480:
			self.y=480
	def fire(self):
		self.BulletList.append(HeroBullet(self.screen,self.x,self.y))
		print(len(self.BulletList))
#玩家子弹定义
class HeroBullet:
	def __init__(self,screen_temp,x,y):
		self.x=x+46
		self.y=y
		self.image=pygame.image.load('./images/images/pd.png')
		self.screen=screen_temp
	def display(self):
		self.screen.blit(self.image,(self.x,self.y))
	def move(self):
		self.y-=10
		if self.y<=-35:
			return True
#敌机定义
class Enemy:
	def __init__(self,screen_temp):
		self.x=random.choice(range(408))
		self.y=-75
		self.image=pygame.image.load('./images/images/e2.png')
		self.screen=screen_temp
		self.BulletList=[]
	def display(self):
		if random.choice(range(15))==1:
			self.fire()
		for bullet in self.BulletList:
			bullet.display()
			if bullet.move():
				self.BulletList.remove(bullet)
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		self.y+=5
		if self.y>=655:
			return True
	def fire(self):
		self.BulletList.append(EnemyBullet(self.screen,self.x,self.y))
#敌机子弹定义
class EnemyBullet:
	def __init__(self,screen_temp,x,y):
		self.x=x+44
		self.y=y+74
		self.image=pygame.image.load('./images/images/epd.png')
		self.screen=screen_temp
	def display(self):
		self.screen.blit(self.image,(self.x,self.y))
	def move(self):
		self.y+=10
		if self.y>=580:
			return True
def main():
	#创建窗口
	screen=pygame.display.set_mode((512,568),0,0)
	#创建游戏背景
	background=pygame.image.load("./images/images/bg1.jpg")
	m=-968
	#玩家创建
	hero=HeroPlane(screen)
	#创建敌机列表
	Enemylist=[]
	while True:
		#绘制位图
		screen.blit(background,(0,m))
		m+=2
		if m>=-200:
			m=-968
		#玩家显示
		hero.display()
		#敌机随机出现
		if random.choice(range(20))==1:
			Enemylist.append(Enemy(screen))
			print(len(Enemylist))
		for enemy in Enemylist:
			enemy.display()
			if enemy.move():
				Enemylist.remove(enemy)
		#调用键盘控制函数
		key_control(hero)
		#更新屏幕显示
		pygame.display.update()
		#定时睡眠（时钟）
		time.sleep(0.04)

if __name__=="__main__":
	main()
